﻿using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using umbraco.presentation.nodeFactory;
using System.Text;
using System.Configuration;
using System.Text.RegularExpressions;
using log4net;
using System.Reflection;

namespace GrailTechnology.AutoTags.DataType
{
    public class Helper
    {
        private static readonly ILog log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);

        static Helper instance = null;

        private Helper()
        {
        }

        public static Helper Instance
        {
            get
            {
                if (instance == null) {
                    instance = new Helper();
                }

                return instance;
            }
        }

        public string RemoveHTML(string html)
        {
            return Regex.Replace(html, @"<[^>]*>", String.Empty);
        }

         public string GetNodeProperties(int id)
         {
             var content = new StringBuilder();
             var currentNode = new Node(id);
             var propertyAliases = GetNodeProperties();
             foreach (Property property in currentNode.Properties)
             {
                 if (propertyAliases[0] == "all" || IsRequiredProperty(propertyAliases, property.Alias))
                 {
                     log.Debug("Found property: " + property.Alias);
                     content.AppendFormat("{0} ", RemoveHTML(property.Value));
                 }
             }

             return content.ToString();
         }

        public string GetFormProperties(NameValueCollection form, List<string> sanitizedKeys)
        {
            var content = new StringBuilder();
            var propertyAliases = GetNodeProperties();
            foreach (string key in sanitizedKeys)
            {
                var split = key.Split('$');
                if (propertyAliases[0] == "all" || IsRequiredProperty(propertyAliases, split[split.Length - 1]))
                {
                    string v = RemoveHTML(form[key]).Trim();
                    if (v.Length > 0)
                    {
                        log.Debug("Adding value from form key: " + key);
                        if (v.EndsWith(".") || v.EndsWith(",") || v.EndsWith("?") || v.EndsWith("!"))
                        {
                            content.AppendFormat("{0} ", v);
                        }
                        else
                        {
                            content.AppendFormat("{0}. ", v);

                        }
                    }
                }
            }

            return content.ToString();
        }

         protected string[] GetNodeProperties()
         {
             if (!string.IsNullOrEmpty(ConfigurationManager.AppSettings["AutoTags.PropertyNames"]))
             {
                 if (ConfigurationManager.AppSettings["AutoTags.PropertyNames"].ToLower() != "all")
                 {
                     return ConfigurationManager.AppSettings["AutoTags.PropertyNames"].Split(';');
                 }
             }

             return new string[1] { "all" };
         }

         protected bool IsRequiredProperty(string[] propertyNames, string propertyName)
         {
             foreach (string property in propertyNames)
             {
                 if (property == propertyName)
                 {
                     return true;
                 }
             }

             return false;
         }
    }
}